package cn.qinyun.cloud.core.feign.fallback;

import feign.Target;
import lombok.AllArgsConstructor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cloud.openfeign.FallbackFactory;

/**
 * 类名称：MateFallbackFactory <br>
 * 类描述：TODO <br>
 *
 * @author 张兆发
 * @date 创建时间：2021/7/16  11:37 上午 <br>
 * @version 1.0.0
 */
@AllArgsConstructor
public class MateFallbackFactory<T> implements FallbackFactory<T> {
    private final Target<T> target;

    @Override
    @SuppressWarnings("unchecked")
    public T create(Throwable cause) {
        final Class<T> targetType = target.type();
        final String targetName = target.name();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetType);
        enhancer.setUseCache(true);
        enhancer.setCallback(new cn.qinyun.cloud.core.feign.fallback.MateFeignFallback<>(targetType, targetName, cause));
        return (T) enhancer.create();
    }
}
